//
//  DHDescribeView.m
//  发;fjag
//
//  Created by 刘鹏飞 on 2016/11/14.
//  Copyright © 2016年 刘鹏飞. All rights reserved.
//

#import "DHDescribeView.h"
#import <Masonry.h>
#import "XHHAdditions.h"
#import "DHDiseaseClassModel.h"

@interface DHDescribeView ()


@property (nonatomic,weak)UILabel *describeLabel;



@property (nonatomic,weak)UIButton *addBotton;

@property (nonatomic,weak)UIView *sepLineViewThree;

@property (nonatomic,copy)NSArray *buttons;




@end

@implementation DHDescribeView

+(instancetype)describeView {
    
    DHDescribeView *describeView = [[DHDescribeView alloc] init];
    //创建label
    UILabel *describeLabel = [UILabel XHH_labelWithText:@"疾病描述:" fontSize:15 color:[UIColor blackColor]];
    UILabel *addLabel = [UILabel XHH_labelWithText:@"添加病症描述标签" fontSize:15 color:[UIColor cyanColor]];
    //开启交互
    addLabel.userInteractionEnabled = YES;
    
    //创建btn
    UIButton *addBotton = [[UIButton alloc] init];
    [addBotton setImage:[UIImage imageNamed:@"add_40x40"] forState:UIControlStateNormal];
    describeView.addBtn = addBotton;
    UITextView *textView = [[UITextView alloc] init];
    textView.backgroundColor = [UIColor XHH_colorWithHex:0xf9f9f9];
    UIView *sepLineViewThree = [[UIView alloc] init];
    sepLineViewThree.backgroundColor = [UIColor XHH_colorWithHex:0xf9f9f9];
    
    //添加到父控件
    [describeView addSubview:describeLabel];
    [describeView addSubview:addBotton];
    [describeView addSubview:addLabel];
    [describeView addSubview:textView];
    [describeView addSubview:sepLineViewThree];
    
    //属性指向
    describeView.addLabel = addLabel;
    describeView.addBotton = addBotton;
    describeView.describeLabel = describeLabel;
    describeView.textView = textView;
    describeView.sepLineViewThree = sepLineViewThree;
    
    
    //布局子视图
    [describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(describeView).offset(8);
        make.top.equalTo(describeView).offset(20);
    }];
    [addBotton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(describeLabel);
        make.right.equalTo(describeView).offset(-8);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [addLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(describeLabel);
        make.right.equalTo(addBotton.mas_left).offset(-8);
    }];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(describeView).offset(20);
        make.bottom.right.equalTo(describeView).offset(-20);
        make.top.equalTo(describeLabel.mas_bottom  ).offset(20);
    }];
    [sepLineViewThree mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.left.equalTo(describeView);
        make.height.mas_equalTo(1);
    }];
    
    return describeView;
    
}

-(void)setTabArr:(NSArray *)tabArr {
    _tabArr = tabArr;
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    for (UIButton *btn in self.buttons) {
        [btn removeFromSuperview];
    }
    if (tabArr.count == 0) {
        //布局子视图
        [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(20);
            make.bottom.right.equalTo(self).offset(-20);
            make.top.equalTo(self.describeLabel.mas_bottom ).offset(20);
            
        }];
    }else {
        NSMutableArray *arrM = [NSMutableArray array];
        for (int i = 0; i < self.tabArr.count; i++) {
            if (i < 4) {
                DHDiseaseClassModel *model = self.tabArr[i];
                UIButton *botton = [[UIButton alloc] init];
                [arrM addObject:botton];
                [self addSubview:botton];
                [botton setTitle:model.diseaseClass forState:UIControlStateNormal];
                botton.backgroundColor = [UIColor cyanColor];
                botton.frame = CGRectMake((width-320)/5*(i+1)+80*i, 50, 80, 20);
            }else {
                DHDiseaseClassModel *model = self.tabArr[i];
                UIButton *botton = [[UIButton alloc] init];
                [arrM addObject:botton];
                [self addSubview:botton];
                [botton setTitle:model.diseaseClass forState:UIControlStateNormal];
                botton.backgroundColor = [UIColor cyanColor];
                botton.frame = CGRectMake((width-320)/5*(i-3)+80*(i-4), 80, 80, 20);
            }
        }
        self.buttons = arrM;
        if (self.tabArr.count <= 4) {
            [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(20);
                make.bottom.right.equalTo(self).offset(-20);
                make.top.equalTo(self.describeLabel.mas_bottom ).offset(50);
                
            }];
        }else {
            [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(20);
                make.bottom.right.equalTo(self).offset(-20);
                make.top.equalTo(self.describeLabel.mas_bottom ).offset(75);
            }];
        }
        
    }
}


@end
